package me.kafeitu.activiti.chapter10;

import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ExecutionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.Deployment;
import org.activiti.spring.impl.test.SpringActivitiTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试callActivity流程
 *
 * @author henryyan
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-test-chapter10.xml")
public class DisposeEcodeCall {
//public class DisposeEcodeCall extends SpringActivitiTestCase {
    @Autowired
    protected RepositoryService repositoryService;
    @Autowired
    protected RuntimeService runtimeService;
    @Autowired
    protected TaskService taskService;
    @Autowired
    protected FormService formService;
    @Autowired
    protected HistoryService historyService;
    @Autowired
    protected IdentityService identityService;
    @Autowired
    protected ManagementService managementService;
    
    @Test
    @Deployment(resources = {"diagrams/aecode2/shi-process.bpmn","diagrams/aecode2/apply-process.bpmn"})
    public void testOne() throws Exception {

        identityService.setAuthenticatedUserId("henryyan");

        Map<String, Object> variables = new HashMap<String, Object>();
        //List<String> users = Arrays.asList("user1", "user2", "user3");
        //variables.put("users", users);
        //List<String> leaders = Arrays.asList("leader1", "leader2", "leader3");
        //variables.put("leaders", leaders);
        //variables.put("shiLeaderAssis","leader1");
        variables.put("shengLeaderAssis","sheng_assis");
        variables.put("user", "xian1");
        variables.put("leader","leader1");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("shiProcess", variables);
        assertNotNull(processInstance);
        System.out.println("11eid=" + processInstance.getId() + ", pid=" + processInstance.getProcessInstanceId());
        
        // 审核文件任务
        ExecutionQuery executionQuery = runtimeService.createExecutionQuery().messageEventSubscriptionName("MSG_HELP");
        Execution execution = executionQuery.singleResult();

        // 触发两次
        Map<String, Object> variables2 = new HashMap<String, Object>();
        variables2.put("user", "xian2");
        runtimeService.messageEventReceived("MSG_HELP", execution.getId(), variables2);
        Map<String, Object> variables3 = new HashMap<String, Object>();
        variables3.put("user", "xian3");
        runtimeService.messageEventReceived("MSG_HELP", execution.getId(), variables3);
        
        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskCandidateOrAssigned("leader1").singleResult();
        // taskService.claim(task.getId(), "bill");
        // set local var
        //taskService.setVariable(task.getId(), "deptLeader", "test1");
        System.out.println("task = " + task);

        List<Task> list = taskService.createTaskQuery().taskName("县申请").list();
        for (Task subTask : list) {
            Map<String, Object> variables22 = new HashMap<String, Object>();
            if (subTask.getAssignee().equals("xian3")) {
                variables22.put("shiLeaderAssis","leader2");
            } else {
                variables22.put("shiLeaderAssis","leader1");
            }
            
            taskService.complete(subTask.getId(), variables22);
        }
        
        //Task endTask = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskCandidateOrAssigned("bill").singleResult();
        //List<Task> listTask = taskService.createTaskQuery().processInstanceId(processInstance.getId()).
        //        taskCandidateOrAssigned("leader1").list();
        List<Task> listTask = taskService.createTaskQuery()
                .taskCandidateOrAssigned("leader1").list();
        
        int rejectCount = 1;
        for (Task curTask:listTask) {
            System.out.println("curTask = " + curTask);
            if (curTask.getTaskDefinitionKey().equals("shiAudit") && rejectCount-- > 0) {
                Map<String, Object> variablesShiLeaderAssis = new HashMap<String, Object>();
                variablesShiLeaderAssis.put("shiLeaderAssisPass",false);
                taskService.complete(curTask.getId(), variablesShiLeaderAssis);
            } 
        }
        // set local var
        //taskService.setVariable(task.getId(), "deptLeader", "endtest");
        //taskService.setVariableLocal(endTask.getId(), "deptLeader", "endtest2");
        // taskService.claim(endTask.getId(), "bill");
        
        // complete 市汇总
        Map<String, Object> variablesShiLeader = new HashMap<String, Object>();
        variablesShiLeader.put("shiLeader","dingz");
        taskService.complete(task.getId(), variablesShiLeader);
        
        // shiApprove
        Task shiApproveTask = taskService.createTaskQuery().processInstanceId(processInstance.getId())
                .taskCandidateOrAssigned("dingz").singleResult();
        taskService.complete(shiApproveTask.getId());

        System.out.println("Ended");
    }

}
